<?php

/**
 * 作者：lucas
 * 链接：
 * 邮箱：dissipator_520@qq.com
 */

/**
 * PackgAction
 *
 * @author lucas
 */
class PackgAction extends BaseAction {

    public function index() {
        $this->display();
    }

    public function getData() {

        $map=$this->condition;

        $Mode = M('packg');
        // echo ($this->order);die;
        $data = $Mode->where($this->condition)->page($this->page, $this->rows)->order($this->order)->select();
        Log::write($Mode->getLastSql());
        $this->returnGridData($data, $Mode->where($this->condition)->count());
    }

    public function toAdd() {

            $Model = M("packg");
            $fields=$Model->getDbFields();
            if ($fields) {
                foreach ($fields as $key ) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '';
                        break; 
                    case 'times':
                        $output .= '';
                        break;  
                    default:
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="" style="width: 150px;"/></td></tr>';
                    }
                }
                $this->assign('output', $output);
            }
        $this->display('add');
    }

    public function doSave() {
        $Model = M("packg");
        if (!$Model->create()) {
            $this->returnStatus(false, $Model->getError());
        } else {
            $Model->startTrans();
            $result = false;
            if (empty($Model->ID)) {
                $result = $Model->add();
            } else {
                $result = $Model->save();
            }
            $Model->commit();
            $this->returnStatus();
        }
    }

    public function doDelete() {
        $ids = $_POST['ids'];
        $array = explode(',', $ids);
        if ($array && count($array) > 0) {
            $Model = M("Packg");
            foreach ($array as $id) {
                $Model->startTrans();
                if ($Model->where(array('ID' => $id))->delete() !== false && $roleUser->where(array('ID' => $id))->delete() !== false) {
                    $Model->commit();
                } else {
                    $Model->rollback();
                }
            }
        }
        $this->returnStatus();
    }

    public function toUpdate() {
        $ID = $_GET['ID'];
        // P($_GET);
        if ($ID) {
            $Model = M("packg");
            $data = $Model->where('ID = %d', $ID)->find();
            if ($data) {
                foreach ($data as $key => $value) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '<input name="ID" required="required" type="hidden" value="' .$value. '" style="width: 150px;"/>';
                        break; 
                    case 'times':
                        $output .= '';
                        break;  
                    default:
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td></tr>';
                    }
                }
                $this->assign('output', $output);
                // $this->assign('data', $data);
            }
        }
        $this->display('add');
    }

}

?>
